package cn.xqj.org.service.impl;

import cn.xqj.basic.service.impl.BaseServiceImpl;

import cn.xqj.org.domain.Department;
import cn.xqj.org.mapper.DepartmentMapper;

import cn.xqj.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;



    @Override
    @Transactional
    public void add(Department department) {
        if (department.getParent_id() != null) {
            departmentMapper.add(department);
            Long id = department.getId();
            Long parentId = department.getParent_id();
            Department parentDepartment = departmentMapper.findOne(parentId);
            String dirPath = parentDepartment.getDirPath();
            String dir = dirPath + "/" + id;
            department.setDirPath(dir);
            departmentMapper.update(department);
        } else {
            departmentMapper.add(department);
            Long id = department.getId();
            department.setDirPath("/"+id);
            departmentMapper.add(department);
        }
    }



    @Override
    @Transactional
    public void update(Department department) {
        if (department.getParent_id() != null) {
            Long id = department.getId();
            Long parentId = department.getParent_id();
            Department parentDepartment = departmentMapper.findOne(parentId);
            String dirPath = parentDepartment.getDirPath();
            String dir = dirPath + "/" + id;
            department.setDirPath(dir);
            departmentMapper.update(department);
        } else {
            Long id = department.getId();
            department.setDirPath("/"+id);
            departmentMapper.update(department);
        }

    }



    @Override
    public List<Department> deptTree() {
        // 查询所有部门信息
        List<Department> allDepartment = departmentMapper.findAll();
        // 创建map集合
        Map<Long, Department> map = new HashMap<>();
        // 循环遍历所有部门信息，将所有部门存入map中（部门id=当前部门对象）
        for (Department department : allDepartment) {
            map.put(department.getId(),department);
        }

        // 准备返回给前端的部门树集合
        List<Department> deptTree = new ArrayList<>();

        // 再次循环遍历所有部门
        for (Department department : allDepartment) {
            // 如果当前部门没有父部门id -- Parent_id
            if (department.getParent_id() == null) {
                // 就把当前部门存进返回给前端的部门树集合中（因为没有父id 所以全是顶级部门）
                deptTree.add(department);
            } else { // 剩下的全是除开顶级部门的部门
                // 获取当前部门的父部门id -- parent_id
                Long parent_id = department.getParent_id();
                // 再通过父部门id去map集合中查找当前部门的父部门
                Department parentDepartment = map.get(parent_id);
                // 再把当前部门存进父部门的children属性中（children是一个list集合）
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
